package org.atovio.mvcConfig.settings;

import org.atovio.mvcConfig.formatter.DeviceFormatter;
import org.atovio.mvcConfig.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 使用配置类的方式配置SpringMVC
 * @author AtoVio, Winter Yuan
 */
@Configuration
public class MvcSettings implements WebMvcConfigurer {

    // 从请求直达视图页面
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 这行见名知意，不写注释了
        registry.addViewController("/welcome").setViewName("index");
    }

    // 配置格式转换器
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DeviceFormatter());
    }

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor())
                .order(1) // 设置执行顺序
                .addPathPatterns("/articles/**") // 拦截的地址
                .excludePathPatterns("/articles/query"); // 不拦截的地址
    }
}
